﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FileToVideo
{
    /// <summary>
    /// Loading.xaml 的交互逻辑
    /// </summary>
    public partial class Loading : Window, IDisposable
    {
        /// <summary>
        /// 进度条最小值
        /// </summary>
        private int minimum = 0;

        /// <summary>
        /// 进度条最大值
        /// </summary>
        private int maximun;

        /// <summary>
        /// 进度条当前百分比
        /// </summary>
        private double progressBarPersent;

        /// <summary>
        /// 内容
        /// </summary>
        private string context;

        /// <summary>
        /// 进度条当前值
        /// </summary>
        private int count;

        /// <summary>
        /// 底部工作
        /// </summary>
        private BackgroundWorker _worker;

        public Loading(int maxCount,string context)
        {
            // 记录值
            this.maximun = maxCount;
            this.minimum = 0;
            this.progressBarPersent = 0;
            this.context = context;
            this.count = 0;

            // 初始化
            InitializeComponent();

            // 更新数据
            this.LoadingText.Text = context;
            this.ProgressBar.Maximum = maxCount;
            this.ProgressBar.Minimum = 0;
            this.ProgressBar.Value = 0;
            this.ProgressBarContent.Content = "0.00%";

            //// 初始化工作
            //InitializeBackgroundWorker();
        }

        ///// <summary>
        ///// 初始化工作
        ///// </summary>
        //private void InitializeBackgroundWorker()
        //{
        //    _worker = new BackgroundWorker
        //    {
        //        WorkerReportsProgress = true,
        //        WorkerSupportsCancellation = true
        //    };

        //    _worker.DoWork += Worker_DoWork;
        //    _worker.ProgressChanged += Worker_ProgressChanged;
        //    _worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
        //}

        private void moveThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Left += e.HorizontalChange;
            Top += e.VerticalChange;
        }

        /// <summary>
        /// 更新进度条
        /// </summary>
        /// <param name="count"></param>
        public void Update(int count)
        {
            this.count = count;
            this.progressBarPersent = (double)count / (double)this.maximun;
            this.ProgressBarContent.Content = $"{this.progressBarPersent*100:F2}%";
            this.ProgressBar.Value = count;
        }

        /// <summary>
        /// 更新内容
        /// </summary>
        /// <param name="content"></param>
        public void Update(string content)
        {
            this.context = content;
            this.LoadingText.Text = context;
        }

        /// <summary>
        /// 更新内容
        /// </summary>
        /// <param name="count"></param>
        /// <param name="maxCount"></param>
        public void Update(int count,int maxCount)
        {
            Update(count);
            this.maximun = maxCount;
            this.ProgressBar.Maximum = maxCount;
        }

        /// <summary>
        /// 双重更新
        /// </summary>
        /// <param name="count"></param>
        /// <param name="content"></param>
        public void Update(int count,string content)
        {
            Update(count);
            Update(content);
        }

        /// <summary>
        /// 三重更新
        /// </summary>
        /// <param name="count"></param>
        /// <param name="maxCount"></param>
        /// <param name="content"></param>
        public void Update(int count, int maxCount, string content)
        {
            Update(count, maxCount);
            Update(content);
        }

        public void Dispose()
        {
            this.Close();
            this._worker?.Dispose();
        }
    }
}
